package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeNewProductMapper;
import com.mall.domain.SmsHomeBrand;
import com.mall.domain.SmsHomeNewProduct;
import com.mall.interface1.api.SmsHomeNewProductServiceApi;
import com.mall.vo.SmsHomePageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeNewProductServiceApiImpl implements SmsHomeNewProductServiceApi {

    @Autowired
    SmsHomeNewProductMapper smsHomeNewProductMapper;

    @Override
    public SmsHomePageBeanVo findByPage(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(productName),SmsHomeNewProduct::getProductName,productName);
        qw.eq(recommendStatus != null, SmsHomeNewProduct::getRecommendStatus, recommendStatus);
        qw.orderByDesc(SmsHomeNewProduct::getSort);
        Page<SmsHomeNewProduct> smsHomeNewProductPage = smsHomeNewProductMapper.selectPage(page, qw);
        return new SmsHomePageBeanVo(pageNum,pageSize, smsHomeNewProductPage.getTotal(), smsHomeNewProductPage.getRecords());
    }

    @Override
    public Object updateRecommendStatus(Long id, Integer recommendStatus) {
            SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductMapper.selectById(id);
            smsHomeNewProduct.setRecommendStatus(recommendStatus);
            smsHomeNewProductMapper.updateById(smsHomeNewProduct);
        return null;
    }

    @Override
    public Object updateSort(Long id, Integer sort) {
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductMapper.selectById(id);
        smsHomeNewProduct.setSort(sort);
        smsHomeNewProductMapper.updateById(smsHomeNewProduct);
        return null;
    }

    @Override
    public void delete(Long id) {
        smsHomeNewProductMapper.deleteById(id);
    }

    @Override
    public void addCreate(SmsHomeNewProduct smsHomeNewProduct) {
        smsHomeNewProductMapper.insert(smsHomeNewProduct);
    }
}
